﻿using SD.Common;
using SD.Infrastructure.DTOBase;
using SD.Infrastructure.WPF.Caliburn.Aspects;
using SD.Infrastructure.WPF.Caliburn.Base;
using SD.Infrastructure.WPF.Extensions;
using SD.Infrastructure.WPF.Interfaces;
using SD.Infrastructure.WPF.Models;
using SlamDunk.MES.IAppService.DTOs.Outputs.ResourceContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.StockContext;
using SlamDunk.MES.IAppService.Interfaces;
using SlamDunk.MES.Presentation.Maps;
using SlamDunk.MES.Presentation.Models.NotifyModels;
using SlamDunk.ValueObjects.Enums;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.ServiceModel.Extensions;
using System.Threading.Tasks;
using System.Windows;

namespace SlamDunk.MES.Client.ViewModels.MaterialLot
{
    /// <summary>
    /// 通用物料批次信息ViewModel
    /// </summary>
    public class SelectMaterialLotViewModel : ScreenBase, IPaginatable
    {
        #region # 字段及构造器
        /// <summary>
        /// 资源定义服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IStockManageContract> _iStockManageContract;

        /// <summary>
        /// 资源定义服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IResourceContract> _iResourceContract;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public SelectMaterialLotViewModel(ServiceProxy<IStockManageContract> iStockManageContract, ServiceProxy<IResourceContract> iResourceContract)
        {
            this._iStockManageContract = iStockManageContract;
            this._iResourceContract = iResourceContract;
            //默认值
            this.PageIndex = 1;
            this.PageSize = 20;
        }

        #endregion

        #region 属性

        #region 页码 —— int PageIndex
        /// <summary>
        /// 页码
        /// </summary>
        [DependencyProperty]
        public int PageIndex { get; set; }
        #endregion

        #region 页容量 —— int PageSize
        /// <summary>
        /// 页容量
        /// </summary>
        [DependencyProperty]
        public int PageSize { get; set; }
        #endregion

        #region 总记录数 —— int RowCount
        /// <summary>
        /// 总记录数
        /// </summary>
        [DependencyProperty]
        public int RowCount { get; set; }
        #endregion

        #region 总页数 —— int PageCount
        /// <summary>
        /// 总页数
        /// </summary>
        [DependencyProperty]
        public int PageCount { get; set; }
        #endregion

        #region 关键字
        /// <summary>
        /// 关键字
        /// </summary>
        [DependencyProperty]
        public string Keywords { get; set; }
        #endregion

        #region 供应商列表
        /// <summary>
        /// 供应商列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<IdentiferNameNm> Suppliers { get; set; }
        #endregion

        #region 物料来源列表
        /// <summary>
        /// 物料来源列表
        /// </summary>
        [DependencyProperty]
        public IDictionary<string, string> MaterialSourceTypes { get; set; }
        #endregion

        #region 开始时间
        /// <summary>
        /// 开始时间
        /// </summary>
        [DependencyProperty]
        public DateTime? StartTime { get; set; }
        #endregion

        #region 结束时间
        /// <summary>
        /// 结束时间
        /// </summary>
        [DependencyProperty]
        public DateTime? EndTime { get; set; }
        #endregion

        #region 已选供应商
        /// <summary>
        /// 已选供应商
        /// </summary>
        [DependencyProperty]
        public IdentiferNameNm SelectedSupplier { get; set; }
        #endregion

        #region 已选物料来源
        /// <summary>
        /// 已选物料来源
        /// </summary>
        [DependencyProperty]
        public MaterialSourceType? SelectedMaterialSourceType { get; set; }
        #endregion

        #region 物料批次列表
        /// <summary>
        /// 物料批次列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<Wrap<MaterialLotInfo>> MaterialLotList { get; set; }
        #endregion

        #region 工厂模型Id
        public Guid? FacilityId { get; set; }
        #endregion

        #region 物料定义Id
        public Guid? MaterialId { get; set; }
        #endregion

        #region 已选物料物料批次
        /// <summary>
        /// 已选物料物料批次
        /// </summary>
        [DependencyProperty]
        public MaterialLotInfo SelectedMaterialLot { get; set; }
        #endregion

        #endregion

        #region 方法

        #region 加载 —— async Task Load(Guid? facilityId, Guid? materialId)
        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="facilityId">工厂模型</param>
        /// <param name="materialId">物料定义</param>
        /// <returns></returns>
        public async Task Load(Guid? facilityId, Guid? materialId)
        {
            FacilityId = facilityId;
            MaterialId = materialId;
            //初始供应商列表
            PageModel<EnterpriseInfo> enterprises = await Task.Run(() =>
                this._iResourceContract.Channel.GetEnterprisesByPage(
                   null,//关键字
                   EnterpriseType.Supplier,//企业类型
                   facilityId,//工厂模型Id
                   1,
                   int.MaxValue)//上级节点Id
             );
            IEnumerable<IdentiferNameNm> enterpriseModels = enterprises.Datas.Select(x => x.ToIdentiferName());
            Suppliers = new ObservableCollection<IdentiferNameNm>(enterpriseModels);
            //初始化物料来源列表
            this.MaterialSourceTypes = typeof(MaterialSourceType).GetEnumMembers();
            //显示物料批次列表
            ReloadMaterialLot();
        }
        #endregion

        #region 搜索
        /// <summary>
        /// 搜索
        /// </summary>
        public async void LoadMaterialLot()
        {
            ReloadMaterialLot();
        }
        #endregion


        #region 提交 —— async void Submit()
        /// <summary>
        /// 提交
        /// </summary>
        public async void Submit()
        {
            #region # 验证
            List<MaterialLotInfo> checkedRoles = this.MaterialLotList.Where(x => x.IsChecked == true).Select(x => x.Model).ToList();
            if (!checkedRoles.Any() || checkedRoles.Count() != 1)
            {
                MessageBox.Show("请勾选一条物料批次！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            #endregion
            SelectedMaterialLot = checkedRoles.FirstOrDefault();
            //this._addItemViewModel.SelectedSupplier = new DictionariesInfo(checkedRoles.FirstOrDefault().Id, checkedRoles.FirstOrDefault().Name);
            //this._addItemViewModel.GuaranteePeriod = Convert.ToInt32(checkedRoles.FirstOrDefault().GuaranteePeriod);
            //this._addItemViewModel.ExpiredDate = checkedRoles.FirstOrDefault().ExpiredDate;
            //this._addItemViewModel.ProductionBatchNo = checkedRoles.FirstOrDefault().ProductionBatchNo;
            //this._addItemViewModel.Specification = checkedRoles.FirstOrDefault().Specification;
            //this._addItemViewModel.SelectedMaterialSourceType = checkedRoles.FirstOrDefault().MaterialSourceType;
            //this._addItemViewModel.ProducedDate = checkedRoles.FirstOrDefault().ProducedDate;
            //this._addItemViewModel.materialLotId = checkedRoles.FirstOrDefault().Id;
            await base.TryCloseAsync(true);
        }
        #endregion

        //Private

        #region 加载物料批次列表 —— async Task ReloadMaterialLot()
        /// <summary>
        /// 加载物料批次列表
        /// </summary>
        private async Task ReloadMaterialLot()
        {
            Guid? SupplierId = null;
            if (SelectedSupplier != null)
            {
                SupplierId = SelectedSupplier.Id;
            }
            this.Busy();
            PageModel<MaterialLotInfo> MaterialLots = await Task.Run(() => this._iStockManageContract.Channel.GetMaterialLotsByPage(
                 Keywords,//关键字
                 MaterialId, //物料定义Id
                 SupplierId, //供应商Id
                 null,//生产日期开始
                 null,//生产日期结束
                 null,//过期日期开始
                 null,//过期日期结束
                 StartTime,//接收日期开始
                 EndTime,//接收日期结束
                 SelectedMaterialSourceType,//物料来源类型
                 FacilityId,//工厂模型Id
                 1,//页码
                 int.MaxValue));
            this.RowCount = MaterialLots.RowCount;
            this.PageCount = MaterialLots.PageCount;
            this.MaterialLotList = new ObservableCollection<Wrap<MaterialLotInfo>>(MaterialLots.Datas.Select(x => x.Wrap()));

            this.Idle();
        }
        #endregion
        #endregion
    }
}


